﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;

using InfFront.UICore;

namespace InfFront.InfoViews.Base
{
    /// <summary>
    /// View-side IInfoCommand wrapper
    /// </summary>
    public class InfoViewCommand : BaseInfoViewEntity, IIconedObject
    {
        #region Private Variables
        private IInfoCommand command;
        private InfoViewSource owner;
        private bool isEnabled = false;
        #endregion

        #region Constrution/Initialization
        internal InfoViewCommand(InfoViewSource owner, IInfoCommand command)
            : base(command.GetName(), owner)
        {
            this.command = command;
            this.owner = owner;
        }

        internal InfoViewSource Owner
        {
            get { return owner; }
        }
        #endregion Constrution/Initialization

        #region Public Properties

        public string Title
        {
            get { return command.GetTitle(); }
        }

        public int Order
        {
            get { return command.GetOrder(); }
        }

        public bool IsEnabled
        {
            get { return isEnabled; }
            set { SetPropertyAndFire<bool>("IsEnabled", value, ref isEnabled); }
        }

        public string Comment
        {
            get { return command.GetComment(); }
        }

        private CommandCallMethod PreferredCallMethod
        {
            get { return command.GetPreferredCallMethod(); } 
        }

        #endregion Public Properties

        #region Execution
        public class ExecutionArgs
        {
            public InfoViewEntry CurrentEntry { get; set; }
        }

        public void Execute(ExecutionArgs args)
        {
            switch (PreferredCallMethod)
            {
                case CommandCallMethod.Synchronous:
                    Owner.ExecuteCommand(command, args, false);
                    break;
                case CommandCallMethod.Asynchronous:
                    Owner.ExecuteCommand(command, args, true);
                    break;
                case CommandCallMethod.InMainThread:
                    Owner.ExecuteCommandInSameThread(command, args);
                    break;
                default:
                    throw new NotSupportedException(string.Format("Unsupported CommandCallMethod: {0}", PreferredCallMethod));
            }
        }
        #endregion Execution

        #region Icons
        private IImage GetIImage(int hSize, int vSize, bool disabled, bool highlighted)
        {
            var args = new InfoCommandGetIconArgs();
            args.HSize = hSize;
            args.VSize = vSize;
            if (disabled)
            {
                if (highlighted) args.Style = IconStyle.DisabledHighlighted;
                else args.Style = IconStyle.Highlighted;
            }
            else
            {
                if (highlighted) args.Style = IconStyle.Highlighted;
                else args.Style = IconStyle.Normal;
            }
            var result = command.GetIcon(args);
            return result;
        }

        public System.IO.Stream GetIconStreamPng(int hSize, int vSize, bool disabled, bool highlighted)
        {
            var result = Owner.RootParent.ExtractIImage(GetIImage(hSize, vSize, disabled, highlighted));
            return result;
        }
        #endregion Icons
    }
}
